﻿using System;
using System.Collections.Generic;
using System.Resources;
using System.Text;
using com.lover.astd.common.config;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;
using com.lover.astd.common.model.building;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.logicexe.building
{
    public class BuildingExe : ExeBase
    {
        public BuildingExe() { _name = ConfigStrings.S_Building; _readable = ConfigStrings.SR_Building; }
        public void getAllBuildings()
        {
            ServiceFactory.getBuildingManager().getBuildingMainCity(_proto, _logger, _user);
            if (_user.Level > 100) ServiceFactory.getBuildingManager().getBuildingsOutCity(_proto, _logger, _user);
            refreshUi();
        }
        private List<int> getBuildingsToBuild()
        {
            Dictionary<string, string> configs = getConfig();
            string confstr = "";
            if (configs.ContainsKey(ConfigStrings.build_ids)) confstr = configs[ConfigStrings.build_ids];
            return generateIds(confstr);
        }
        public override void init_data() { getAllBuildings(); }
        public override long execute()
        {
            Dictionary<string, string> configs = getConfig();
            if (!configs.ContainsKey(ConfigStrings.enabled) || !configs[ConfigStrings.enabled].ToLower().Equals("true")) return an_hour_later();
            bool _enable_protect = configs.ContainsKey(ConfigStrings.protect) && configs[ConfigStrings.protect].ToLower().Equals("true");
            // 判断是否是考古时间
            if (_user.isActivityRunning(ActivityType.ArchEvent))
            {
                // 判断驿站是否已经升级满, 如果还没有升级满, 那么暂停升级其他建筑
                Building b = ServiceFactory.getBuildingManager().getBuilding(_user._buildings, "驿站");
                if (b != null && b.Level < _user.Level) { logInfo("当前为考古时间, 驿站尚未升满, 暂停其他建筑"); return next_hour(); }
            }

            getAllBuildings();
            
            if (!ServiceFactory.getBuildingManager().hasEmptyLine(_user._buildingLines))
            {
                long recent_cd = ServiceFactory.getBuildingManager().recentLineCd(_user._buildingLines);
                return recent_cd;
            }
            int silver_available = getSilverAvailable();
            int stone_available = getStoneAvailable();
            List<int> buildids_tobuild = getBuildingsToBuild();
            Building build = null;

            // 如果开启建筑保护, 那么需要首先找出最低的民居是多少级
            int min_house_level = 1000000;
            int colletor_level = 100000; // 外城采集场等级, 其他外城建筑必须比采集场低
            if (_enable_protect)
            {
                foreach (Building tmpb in _user._buildings)
                {
                    if (tmpb.IsOutCity)
                    {
                        if (tmpb.Name.IndexOf("采集场") >= 0) colletor_level = tmpb.Level;
                        continue;
                    }
                    if (tmpb.Name.IndexOf("民居") < 0) continue;
                    if (tmpb.Level < min_house_level) min_house_level = tmpb.Level;
                }
            }
            else
            {
                foreach (Building tmpb in _user._buildings)
                {
                    if (tmpb.IsOutCity)
                    {
                        if (tmpb.Name.IndexOf("采集场") >= 0) colletor_level = tmpb.Level;
                        break;
                    }
                }

            }

            stone_available = stone_available * 1000; // 外城升级建筑消耗的单位变成了原石
            foreach (int buildid in buildids_tobuild)
            {
                Building b = ServiceFactory.getBuildingManager().getBuilding(_user._buildings, buildid);
                if (b == null) continue;
                if (b.Level == _user.Level) continue;
                // 不升级监狱
                if (b.Name.IndexOf("监狱") >= 0) continue;
                if (b.IsOutCity)
                {
                    if (b.Name.IndexOf("采集场") < 0 && b.Level == colletor_level) continue;
                }
                if (_enable_protect) // 如果开启保护, 那么内城其他建筑会比最低级别的民居的级别低一级
                {
                    if (!b.IsOutCity && b.Name.IndexOf("民居") < 0 && b.Level >= min_house_level - 1) continue;
                }

                if (b.IsOutCity && stone_available < b.UpgradeCost) continue;
                if (!b.IsOutCity && silver_available < b.UpgradeCost) continue;
                build = b;
                break;
            }
            if (build == null) return next_halfhour();
            ServiceFactory.getBuildingManager().upgradeBuilding(_proto, _logger, build, _user._buildingLines);
            return immediate();
        }
    }
}
